<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;


class AttendanceController extends AdminController
{
    const MODEL_NAME = 'Attendance@:';

    //考勤管理-考勤列表首页
    public function index()
    {   
    header("Content-type: text/html; charset=utf-8");

        $model = D('core_attendance');      //存入数据库中，进行排序并显示页面
        $map['status'] = array('neq','-1');
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);    //分页、
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
        $leave=M('core_unit')->where('id = '.$value['unit'])->getField('title');
        $list[$key]['unit'] = $leave;
        $leava=M('core_userinfo_view')->where('uid = '.$value['uid'])->getField('nickname');
        $list[$key]['uid'] = $leava;
        $leass=M('core_class')->where('id = '.$value['class_id'])->getField('title');
        $list[$key]['class_id'] = $leass;

        }
        $this->assign('_list',$list);
        $this->display(self::MODEL_NAME.'Attendance/index'); 
        
    }
    //    public function addrecord()
    // {
    //     if(IS_POST){
    //         $a=strtotime(I('createtime'));
    //         $b=strtotime(I('endDate'));
    //         $arr=array($a,$b);
    //         $dd['leave_date']=implode(",",$arr);
    //         $dd['create_time']=time();
    //         $dd['unit']=I('unit');
    //         $dd['due_num']=I('due_num');
    //         $dd['miss_num']=I('miss_num');
    //         $dd['uid']=I('uid');
    //         $d=M('core_attendance')->add($dd);
    //         $this->success('添加成功',U('index'));
    //     }else{
    //         $typeList = M('user_role')->select();
            
    //         $this->assign('typeList',$typeList);
    //         $this->meta_title = '新增接口';
    //         $this->display(self::MODEL_NAME.'Attendance/addrecord');   
    //     }

    // }
     
    // public function type()
    // {   
    //     if(I('keyword')){
    //         $keyword = I('keyword');
    //         $map['title'] = array('like','%'.$keyword.'%');
    //         $this->assign('keyword',$keyword);
    //     }
    //     if(I('sort')){
    //         $sort = I('sort');
    //         if($sort == 'recycle'){
    //             $map['status'] = -1;

    //             $this->assign('recycle','【回收站】');
    //         }
    //     }else{
    //         $sort = 'normal';
    //         $map['status'] = array('egt', 0);
    //     }
    //     $model = D('CoreInterfaceType');
    //     $totalCount = $model->where($map)->count();
    //     $page = new \Think\Page($totalCount,10);
    //     $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
    //     $list = $model->where($map)->limit($limit)->order('sort,id desc')->select();
    //     $this->assign('_list',$list);
    //     $this->assign('_page',$page->show());
    //     $this->assign('sort',$sort);
    //     $this->meta_title = '接口类型';
    //     $this->display(self::MODEL_NAME.'Type/index'); 
        
    // }
    //  public function addType()
    // {
    //     if(IS_POST){
    //         if(!I('post.title')) $this->error('请输入名称!');
    //         $model_object = D('CoreInterfaceType');
    //         $model_object->title = trim(I('post.title'));
    //         $model_object->remark = trim(I('post.remark'));
    //         $model_object->status = 1;
    //         $model_object->create_time = NOW_TIME;
    //         if($model_object->add()){
    //             $this->success('新增成功!',U('type'));
    //         }else{
    //             $this->error('新增失败!');
    //         }
    //     }

        
    //     $this->meta_title = '新增类型';
    //     $this->display(self::MODEL_NAME.'Type/addtype');

    // }
    public function editType()
    {
        if(IS_POST){
            //dump($_POST);exit;
            $id = I('post.id');
            $model_object = D('core_attendance');
            $model_object->uid = trim(I('post.uid'));
            $model_object->class_id = trim(I('post.class_id'));
            $model_object->due_num = trim(I('post.due_num'));
            $model_object->miss_num = trim(I('post.miss_num'));
            $model_object->update_time = NOW_TIME;
            $model_object->id = $id;
            if($model_object->save()){
                $this->success('更新成功!',U('index'));
            }else{
                $this->error('更新失败!');
            }
        }

        $id = I('get.id');
        $model_object = D('core_attendance');
        $data = $model_object->find($id);
        $this->assign('data',$data);
        $this->meta_title = '编辑类型';

        $this->typeList = M('core_unit')->select();
        $this->typeLis = M('core_userinfo_view')->where(array('unit_id'=>$data['unit'],'group_id'=>4))->select();
        $this->typeLi = M('core_class')->where(array('unit'=>$data['unit']))->select();
        //dump($typeLi);exit;

        $this->display(self::MODEL_NAME.'Attendance/editType');

    }
    public function statist()
    {
        if (I('unit') != 0) {
            $map['unit'] = I('unit');
            $data = M('core_unit')->where('id = '.I('unit'))->getField('title');
            $statustitle =  $statustitle ."服务类型为 ".$data."<br />";
        }
        if (I('uid') != 0) {
            $map['uid'] = I('uid');
            $data = M('core_userinfo_view')->where('uid = '.I('uid'))->getField('nickname');
            $statustitle =  $statustitle ."服务类型为 ".$data."<br />";
        }
        if(I('class_id') != 0){
            $map['class_id'] = I('class_id');
            $data = M('core_class')->where('id = '.I('class_id'))->getField('title');
            $statustitle =  $statustitle ."服务类型为 ".$data."<br />";
        }
        if (I('start') && I('end')) {

            $map['create_time'] = array(between, array(strtotime(I('start')), strtotime(I('end'))));
            $statustitle        = $statustitle."从" . I('start') . "到" . I('end') . "<br />";
        }
        $params = array(
            'unit'      => I('unit'),
            'uid'      => I('uid'),
            'class_id'      => I('class_id'),
            'create_time'      => I('start'),
        );

            //dump(I('uid'));exit;
        // $map['status'] = array('neq','-1');
        $model         = D('core_attendance');      //存入数据库中，进行排序并显示页面
        $totalCount    = $model->where($map)->count();
        //$page          = new \Think\Page($totalCount, 10,$params);
        $limit         = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list          = $model->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $leave     =M('core_unit')->where('id = '.$value['unit'])->getField('title');
            $list[$key]['unit'] = $leave;

            $leava     =M('core_userinfo_view')->where('uid = '.$value['uid'])->getField('nickname');
            $list[$key]['uid'] = $leava;

            $leass     =M('core_class')->where('id = '.$value['class_id'])->getField('title');
            $list[$key]['class_id'] = $leass;

        }

        //
        $this->assign('statustitle', $statustitle);
        $this->assign('totalCount', $totalCount);
        $this->unit = M('core_unit')->select();
        $this->uid = M('core_userinfo_view')->where('group_id = 4')->select();
        $this->class_id = M('core_class')->select();
        $this->assign('_list',$list);
        $this->display(self::MODEL_NAME.'Attendance/statist');
    }

    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
}
